import Vue from "vue";
import VueRouter from "vue-router";
import { store } from "../store/index"; // 引入 store
Vue.use(VueRouter);

const Nxt = () => import("../views/NXT.vue");
const GPX = () => import("../views/KanBan/GPX.vue");
const GPX7 = () => import("../views/KanBan/GPX7.vue");
const GPXMachineEventHis = () => import("../views/KanBan/GPXMachineEventHistory.vue");
const errorPage = () => import("../views/error-page/404.vue");
const GPXsingle = () => import("../views/KanBan/GPXsingle.vue");
const GPXsingle7 = () => import("../views/KanBan/GPXsingle7.vue");
const lineEquipment = () => import("../views/systemManage/lineEquipment/index.vue");
const machineType = () => import("../views/systemManage/machineTypeManage/index.vue");
const systemStatus = () => import("../views/systemManage/systemStatus/index.vue");
const GPXMateriel = () => import("../views/GPXMaterielManage/GPXMateriel.vue");
const GPXMaterielType = () => import("../views/GPXMaterielManage/materielType.vue");
const GPXMaterielCategory = () => import("../views/GPXMaterielManage/materieCategory.vue");
const NXTMaterielType = () => import("../views/NXTMaterielManage/materielType.vue");
const NXTMaterielCategory = () => import("../views/NXTMaterielManage/materieCategory.vue");
const Home = () => import("../views/home/home.vue");
const Layout = () => import("../views/Lyout/index.vue");
const routerView = {
  render: (h) => h("routerView"),
};
// 获取 Vuex 中的 useCustomTheme
const useCustomTheme = store.state.theme.useCustomTheme;
const routes = [];
if (useCustomTheme) {
  if (useCustomTheme === "7-gpx") {
    routes.push(
      ...[
        {
          path: "/spa", // 给主路由添加 spa 前缀
          redirect: { name: "Home" },
          component: Layout,
          meta: { title: "首页", icon: "el-icon-s-home" },
          children: [
            {
              path: "board",
              meta: { title: "看板系统", icon: "el-icon-s-data" },
              component: routerView,
              children: [
                {
                  name: "Home",
                  path: "GPX",
                  component: useCustomTheme ? GPX7 : GPX,
                  meta: { title: "GPX看板" },
                },
                {
                  path: "GPXSingle",
                  component: useCustomTheme ? GPXsingle7 : GPXsingle,
                  meta: { title: "GPX单个看板" },
                },
                {
                  path: "GPXMachineEventHis",
                  component: GPXMachineEventHis,
                  meta: { title: "GPX Machine Events" },
                },
              ],
            },
            {
              path: "gpx-materiel",
              component: routerView,
              meta: { title: "GPX物料物品", icon: "el-icon-s-cooperation" },
              children: [
                {
                  path: "GPXMateriel",
                  component: GPXMateriel,
                  meta: { title: "GPX物品管理", materielCTCategory: "GPX", materielSDUCategory: "GPX" },
                },
                {
                  path: "materielType",
                  component: GPXMaterielType,
                  meta: { title: "物品类型/规格", materielCTCategory: "GPX" },
                },
                {
                  path: "materielCategory",
                  component: GPXMaterielCategory,
                  meta: { title: "物品种类", materielCTCategory: "GPX" },
                },
              ],
            },
            {
              path: "factoryinfo",
              meta: { title: "产线设备&系统配置", icon: "el-icon-s-platform" },
              component: routerView,
              children: [
                {
                  path: "equipment",
                  component: lineEquipment,
                  meta: { title: "线体机器管理" },
                },
                {
                  path: "machine",
                  component: machineType,
                  meta: { title: "设备类型管理" },
                },
                {
                  path: "GPXServeStatus",
                  component: systemStatus,
                  meta: { title: "GPX Serve状态" },
                },
              ],
            },
          ],
        },
        {
          path: "/",
          redirect: { name: "Home" },
          component: Layout,
        },
        {
          path: "*", // 404 页面捕获所有未定义路径
          component: errorPage,
        },
      ]
    );
  }

  if (useCustomTheme === "7-nxt") {
    routes.push(
      ...[
        {
          path: "/spa", // 给主路由添加 spa 前缀
          redirect: { name: "Home" },
          component: Layout,
          meta: { title: "首页", icon: "el-icon-s-home" },
          children: [
            {
              path: "module",
              component: routerView,
              name: "Home",
              meta: { title: "Module维护", icon: "iconfont icon-weihu" },
              children: [
                {
                  path: "moduleMaintain",
                  component: () => import("@/views/ModuleMaintain/ModuleMaintain.vue"),
                  meta: { title: "Module维护信息" },
                },
                //   {
                //     path: "moduleMaintainRecord",
                //     component: () => import("@/views/ModuleMaintain/record/ModuleMaintainRecord.vue"),
                //     meta: { title: "Module保养综合管理" },
                //   },
                {
                  path: "moduleMaintainAdd",
                  component: () => import("@/views/ModuleMaintain/moduleInfoAdd/module.vue"),
                  meta: { title: "Module可视化监控" },
                },
              ],
            },
            {
              path: "head",
              component: routerView,
              meta: { title: "Head维护", icon: "iconfont icon-weihu" },
              children: [
                {
                  path: "headMaintain",
                  component: () => import("@/views/HeadMaintain/HeadMaintain.vue"),
                  meta: { title: "Head保养可视化监控" },
                },
                {
                  path: "headMaintainRecord",
                  component: () => import("@/views/HeadMainteRecord/Maintain.vue"),
                  meta: { title: "Head保养综合管理" },
                },
                {
                  path: "headMaintainRecordScan", // 主要是保养管理登记
                  component: () => import("@/views/HeadMainteRecord/HeadSN/index.vue"),
                  meta: { title: "Head保养管理(扫抢)" },
                },
                {
                  path: "headMaintainRecordHis",
                  component: () => import("@/views/HeadMainteRecord/MaintainHistory.vue"),
                  meta: { title: "Head保养历史记录" },
                },
              ],
            },
            {
              path: "nxt-materiel",
              component: routerView,
              meta: { title: "NXT物料物品", icon: "el-icon-s-cooperation" },
              children: [
                {
                  path: "nozzleMaintain",
                  component: () => import("@/views/NXTMaterielManage/NozzleMateriel.vue"),
                  meta: { title: "Nozzle保养监控", materielCTCategory: "NXT", materielSDUCategory: "Nozzle" },
                },
                {
                  path: "materielType",
                  component: NXTMaterielType,
                  meta: { title: "物品类型/规格", materielCTCategory: "NXT" },
                },
                {
                  path: "materielCategory",
                  component: NXTMaterielCategory,
                  meta: { title: "物品种类", materielCTCategory: "NXT" },
                },
                {
                  path: "nozzleMaintainHis",
                  component: () => import("@/views/NXTMaterielManage/NozzleMaterielHistory.vue"),
                  meta: { title: "Nozzle保养历史记录", materielCTCategory: "NXT", materielSDUCategory: "Nozzle" },
                },
              ],
            },
            {
              path: "factoryinfo",
              meta: { title: "产线设备&系统配置", icon: "el-icon-s-platform" },
              component: routerView,
              children: [
                {
                  path: "equipment",
                  component: lineEquipment,
                  meta: { title: "线体机器管理" },
                },
                {
                  path: "machine",
                  component: machineType,
                  meta: { title: "设备类型管理" },
                },
                {
                  path: "AHCSNCStatus",
                  component: () => import("@/views/systemManage/AHC-SNC/index.vue"),
                  meta: { title: "AHC SNC获取状态" },
                },
                {
                  path: "FujidbStatus",
                  component: () => import("@/views/systemManage/FujiDBGrab/index.vue"),
                  meta: { title: "FujiDB获取状态" },
                },
              ],
            },
            // {
            //   path: "feeder",
            //   component: () => import("@/views/maintenanceManage/Feeder.vue"),
            //   meta: { title: "Feeder" },
            // },
            // {
            //   path: "device",
            //   component: () => import("@/views/maintenanceManage/Device.vue"),
            //   meta: { title: "device" },
            // },
            // {
            //   path: "Nozzle",
            //   component: () => import("@/views/maintenanceManage/Nozzle.vue"),
            //   meta: { title: "Nozzle" },
            // },
          ],
        },
        {
          path: "/",
          redirect: { name: "Home" },
          component: Layout,
        },
        {
          path: "*", // 404 页面捕获所有未定义路径
          component: errorPage,
        },
      ]
    );
  }
} else {
  routes.push(
    ...[
      {
        path: "/spa", // 给主路由添加 spa 前缀
        redirect: { name: "Home" },
        component: Layout,
        meta: { title: "首页", icon: "el-icon-s-home" },
        children: [
          {
            path: "home",
            name: "Home",
            component: Home,
            meta: { title: "首页", icon: "el-icon-s-home" },
          },
          {
            path: "board",
            meta: { title: "看板系统", icon: "el-icon-s-data" },
            component: routerView,
            children: [
              {
                path: "GPX",
                component: useCustomTheme ? GPX7 : GPX,
                meta: { title: "GPX看板" },
              },
              {
                path: "GPXSingle",
                component: useCustomTheme ? GPXsingle7 : GPXsingle,
                meta: { title: "GPX单个看板" },
              },
              {
                path: "GPXMachineEventHis",
                component: GPXMachineEventHis,
                meta: { title: "GPX Machine Events" },
              },
            ],
          },
          {
            path: "gpx-materiel",
            component: routerView,
            meta: { title: "GPX物料物品", icon: "el-icon-s-cooperation" },
            children: [
              {
                path: "GPXMateriel",
                component: GPXMateriel,
                meta: { title: "GPX物品管理", materielCTCategory: "GPX", materielSDUCategory: "GPX" },
              },
              {
                path: "materielType",
                component: GPXMaterielType,
                meta: { title: "物品类型/规格", materielCTCategory: "GPX" },
              },
              {
                path: "materielCategory",
                component: GPXMaterielCategory,
                meta: { title: "物品种类", materielCTCategory: "GPX" },
              },
            ],
          },
          {
            path: "module",
            component: routerView,
            meta: { title: "Module维护", icon: "iconfont icon-weihu" },
            children: [
              {
                path: "moduleMaintain",
                component: () => import("@/views/ModuleMaintain/ModuleMaintain.vue"),
                meta: { title: "Module维护信息" },
              },
              //   {
              //     path: "moduleMaintainRecord",
              //     component: () => import("@/views/ModuleMaintain/record/ModuleMaintainRecord.vue"),
              //     meta: { title: "Module保养综合管理" },
              //   },
              {
                path: "moduleMaintainAdd",
                component: () => import("@/views/ModuleMaintain/moduleInfoAdd/module.vue"),
                meta: { title: "Module可视化监控" },
              },
            ],
          },
          {
            path: "head",
            component: routerView,
            meta: { title: "Head维护", icon: "iconfont icon-weihu" },
            children: [
              {
                path: "headMaintain",
                component: () => import("@/views/HeadMaintain/HeadMaintain.vue"),
                meta: { title: "Head保养可视化监控" },
              },
              {
                path: "headMaintainRecord",
                component: () => import("@/views/HeadMainteRecord/Maintain.vue"),
                meta: { title: "Head保养综合管理" },
              },
              {
                path: "headMaintainRecordScan", // 主要是保养管理登记
                component: () => import("@/views/HeadMainteRecord/HeadSN/index.vue"),
                meta: { title: "Head保养管理(扫抢)" },
              },
              {
                path: "headMaintainRecordHis",
                component: () => import("@/views/HeadMainteRecord/MaintainHistory.vue"),
                meta: { title: "Head保养历史记录" },
              },
            ],
          },
          {
            path: "nxt-materiel",
            component: routerView,
            meta: { title: "NXT物料物品", icon: "el-icon-s-cooperation" },
            children: [
              {
                path: "nozzleMaintain",
                component: () => import("@/views/NXTMaterielManage/NozzleMateriel.vue"),
                meta: { title: "Nozzle保养监控", materielCTCategory: "NXT", materielSDUCategory: "Nozzle" },
              },
              {
                path: "materielType",
                component: NXTMaterielType,
                meta: { title: "物品类型/规格", materielCTCategory: "NXT" },
              },
              {
                path: "materielCategory",
                component: NXTMaterielCategory,
                meta: { title: "物品种类", materielCTCategory: "NXT" },
              },
              {
                path: "nozzleMaintainHis",
                component: () => import("@/views/NXTMaterielManage/NozzleMaterielHistory.vue"),
                meta: { title: "Nozzle保养历史记录", materielCTCategory: "NXT", materielSDUCategory: "Nozzle" },
              },
            ],
          },
          {
            path: "factoryinfo",
            meta: { title: "产线设备&系统配置", icon: "el-icon-s-platform" },
            component: routerView,
            children: [
              {
                path: "equipment",
                component: lineEquipment,
                meta: { title: "线体机器管理" },
              },
              {
                path: "machine",
                component: machineType,
                meta: { title: "设备类型管理" },
              },
              {
                path: "GPXServeStatus",
                component: systemStatus,
                meta: { title: "GPX Serve状态" },
              },
              {
                path: "AHCSNCStatus",
                component: () => import("@/views/systemManage/AHC-SNC/index.vue"),
                meta: { title: "AHC SNC获取状态" },
              },
              {
                path: "FujidbStatus",
                component: () => import("@/views/systemManage/FujiDBGrab/index.vue"),
                meta: { title: "FujiDB获取状态" },
              },
            ],
          },
          // {
          //   path: "feeder",
          //   component: () => import("@/views/maintenanceManage/Feeder.vue"),
          //   meta: { title: "Feeder" },
          // },
          // {
          //   path: "device",
          //   component: () => import("@/views/maintenanceManage/Device.vue"),
          //   meta: { title: "device" },
          // },
          // {
          //   path: "Nozzle",
          //   component: () => import("@/views/maintenanceManage/Nozzle.vue"),
          //   meta: { title: "Nozzle" },
          // },
        ],
      },
      {
        path: "/",
        redirect: { name: "Home" },
        component: Layout,
      },
      {
        path: "*", // 404 页面捕获所有未定义路径
        component: errorPage,
      },
    ]
  );
}

const phoneRoutes = [
  {
    path: "/Device",
    component: () => import("@/views/maintenanceManage/Device.vue"),
    meta: { title: "Feeder" },
  },
  {
    path: "/feeder",
    component: () => import("@/views/maintenanceManage/Feeder.vue"),
    meta: { title: "Feeder" },
  },
  {
    path: "/Nozzle",
    component: () => import("@/views/maintenanceManage/Nozzle.vue"),
    meta: { title: "Feeder" },
  },
  {
    path: "/spa/mobile/KanBan", // 给主路由添加 spa 前缀
    redirect: { name: "MobileKanBanHome" },
    children: [
      {
        // path: "home",
        path: "",
        name: "MobileKanBanHome",
        component: () => import("@/Mobile/KanBan/homePage.vue"),
        meta: { title: "保养维护首页" },
      },
    ],
  },
  {
    path: "/spa/mobile/mainte", // 给主路由添加 spa 前缀
    redirect: { name: "MobileMainteHome" },
    children: [
      {
        // path: "home",
        path: "",
        name: "MobileMainteHome",
        component: () => import("@/Mobile/mainte/homePage.vue"),
        meta: { title: "保养维护首页" },
      },
      {
        path: "feeder",
        component: () => import("@/Mobile/mainte/feeder.vue"),
        meta: { title: "Feeder保养登记" },
      },
      {
        path: "nozzle",
        component: () => import("@/Mobile/mainte/nozzle.vue"),
        meta: { title: "Nozzle保养登记" },
      },
      {
        path: "headView",
        component: () => import("@/Mobile/mainte/headView.vue"),
        meta: { title: "Head保养查看" },
      },
      {
        path: "addHeadMaintain",
        component: () => import("@/Mobile/mainte/addHeadMaintain.vue"),
        meta: { title: "新增保养" },
      },
      {
        path: "module",
        component: () => import("@/Mobile/mainte/module.vue"),
        meta: { title: "module维护" },
      },
      {
        path: "gpx",
        component: () => import("@/Mobile/KanBan/gpx.vue"),
        meta: { title: "GPX单个看板" },
      },
    ],
  },
];

// 平坦子路由
const _phoneRoutes = [];
for (const phoneRoute of phoneRoutes) {
  if (phoneRoute.children) {
    for (const c of phoneRoute.children) {
      c.path = phoneRoute.path + "/" + c.path;
      _phoneRoutes.push(c);
    }
  } else {
    _phoneRoutes.push(phoneRoute);
  }
}

const router = new VueRouter({
  routes: [...routes, ..._phoneRoutes],
  mode: "history",
});

router.afterEach((to, from) => {
  if (to.path.indexOf("mobile") > 0) {
    store.state.auth.needAuth = false;
  } else {
    store.state.auth.needAuth = true;
  }
});

export default router;
